package com.example.mychat.Pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.io.Serializable;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class chatInfo implements Serializable {
    private String chatid;
    private String userid;
    private String chatname;
    private String[] chatdata;

    public static class ChatDataArrayTypeHandler extends BaseTypeHandler<String[]> {
        @Override
        public void setNonNullParameter(PreparedStatement ps, int i, String[] parameter, JdbcType jdbcType) throws SQLException {
            ps.setString(i, String.join(",", parameter));
        }

        @Override
        public String[] getNullableResult(ResultSet rs, String columnName) throws SQLException {
            String value = rs.getString(columnName);
            return value == null ? null : value.split(",");
        }

        @Override
        public String[] getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
            String value = rs.getString(columnIndex);
            return value == null ? null : value.split(",");
        }

        @Override
        public String[] getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
            String value = cs.getString(columnIndex);
            return value == null ? null : value.split(",");
        }
    }
}
